package com.chinaunicom.emergency.repository;

import com.chinaunicom.emergency.model.Logs;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;


/**
 * @author liliang
 * @date 2019-12-13
 * 角色
 */
@Repository
public interface LogsRepository extends JpaRepository<Logs,String>, JpaSpecificationExecutor {

	Logs findByPidAndIsSoftDel(String pid, Integer isSoftDel);

//	Logs findByRoleCodeAndIsSoftDel(String roleCode,Integer isSoftDel);
//
//	Logs findByRoleNameAndIsSoftDel(String roleName,Integer isSoftDel);

    List<Logs> findAllByIsSoftDel(Integer i);


    @Query(nativeQuery = true, value =
            "SELECT logs.* FROM " +
                    " YJGL_LOGS logs LEFT JOIN YJGL_USER users ON logs.CREATE_BY = users.USER_NAME " +
                    "        WHERE logs.IS_SOFT_DEL = ?1 AND if('' != ?2 ,logs.LOG_DESCRIBE LIKE %?2% , 1=1) " +
                    "           AND if('' != ?3 ,logs.LOG_TYPE = ?3, 1=1) AND if('' != ?4, users.REAL_NAME LIKE %?4% ,1=1) " +
                    "           AND if(null != ?5 ,logs.CREATE_TIME >= ?5, 1=1) AND logs.CREATE_TIME < ?6 " +
                    "                ORDER BY logs.CREATE_TIME ASC ",
            countQuery =
                    "SELECT COUNT(logs.pid) FROM" +
                            " YJGL_LOGS logs LEFT JOIN YJGL_USER users ON logs.CREATE_BY = users.USER_NAME " +
                            "        WHERE logs.IS_SOFT_DEL = ?1 AND if('' != ?2 ,logs.LOG_DESCRIBE LIKE %?2% , 1=1) " +
                            "           AND if('' != ?3 ,logs.LOG_TYPE = ?3, 1=1)  AND if('' != ?4, users.REAL_NAME LIKE %?4% ,1=1) " +
                            "           AND if(null != ?5 ,logs.CREATE_TIME >= ?5, 1=1) AND logs.CREATE_TIME < ?6 " )
    Page<Logs> findPageByQuery(Integer isSoftDel, String logDescribe, String logType, String createUserName, Date startDate, Date endDate, Pageable pageable);



}
